/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package org.springblade.modules.mes.base.controller;

import cn.hutool.core.util.IdUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.AllArgsConstructor;
import org.jetbrains.annotations.NotNull;
import org.springblade.core.boot.ctrl.BladeController;
import org.springblade.core.mp.support.Condition;
import org.springblade.core.mp.support.Query;
import org.springblade.core.tool.api.R;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.mes.base.entity.ProduceStation;
import org.springblade.modules.mes.base.service.IProduceStationService;
import org.springblade.modules.mes.base.utils.StringUtils;
import org.springblade.modules.mes.base.vo.ProduceStationVO;
import org.springblade.modules.mes.opcua.constant.OpcUaCache;
import org.springblade.modules.system.util.QueryParaUtil;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * 生产工位 控制器
 *
 * @author BladeX
 * @since 2020-09-10
 */
@RestController
@AllArgsConstructor
@RequestMapping("/produceStation")
@Api(value = "生产工位", tags = "生产工位接口")
public class ProduceStationController extends BladeController {

	private final IProduceStationService produceStationService;

	/**
	 * 详情
	 */
	@GetMapping("/detail")
	//@ApiOperationSupport(order = 1)
	@ApiOperation(value = "详情", notes = "传入produceStation")
	public R<ProduceStation> detail(ProduceStation produceStation) {
		ProduceStation detail = produceStationService.getOne(Condition.getQueryWrapper(produceStation));
		return R.data(detail);
	}

	/**
	 * 不分页 生产工位
	 */
	@GetMapping("/list")
	//@ApiOperationSupport(order = 2)
	@ApiOperation(value = "不分页", notes = "传入produceLine")
	public R<List<ProduceStation>> list(ProduceStationVO produceStationVO) {
		ProduceStationVO cond = buildCond(produceStationVO);
		List<ProduceStation> produceLineVOList = produceStationService.list(Condition.getQueryWrapper(cond));
		return R.data(produceLineVOList);
	}

	/**
	 * 根据产线查询工位
	 */
	@GetMapping("/listByProductionLine")
	//@ApiOperationSupport(order = 2)
	@ApiOperation(value = "根据产线查询工位", notes = "传入produceLine")
	public R<List<ProduceStation>> listByProductionLine(@RequestParam String productionLine) {
		if (StringUtils.isEmpty(productionLine)) {
			return R.fail("生产线不能为空！");
		}
		List<ProduceStation> produceStationVOList = produceStationService.listByProductionLine(Func.toStrList(productionLine));
		return R.data(produceStationVOList);
	}
//	@GetMapping("/listByProductionLine")
//	//@ApiOperationSupport(order = 2)
//	@ApiOperation(value = "根据产线查询工位", notes = "传入produceLine")
//	public R<List<ProduceStation>> listByProductionLine(@RequestParam String productionLine) {
//		LambdaQueryWrapper<ProduceStation> wrapper = new LambdaQueryWrapper<>();
//		wrapper.in(ProduceStation::getProductionLine,Func.toStrList(productionLine));
//		List<ProduceStation> produceStationVOList = produceStationService.list(wrapper);
//		return R.data(produceStationVOList);
//	}

	/**
	 * 自定义分页 生产工位
	 */
	@GetMapping("/page")
	//@ApiOperationSupport(order = 3)
	@ApiOperation(value = "分页", notes = "传入produceStation")
	public R<IPage<ProduceStationVO>> page(ProduceStationVO produceStationVO, Query query) {
		ProduceStationVO cond = buildCond(produceStationVO);
		IPage<ProduceStationVO> pages = produceStationService.selectProduceStationPage(Condition.getPage(query), cond);
		return R.data(pages);
	}


	/**
	 * 新增 生产工位
	 */
	@PostMapping("/save")
	//@ApiOperationSupport(order = 4)
	@ApiOperation(value = "新增", notes = "传入produceStation")
	public R save(@Valid @RequestBody ProduceStation produceStation) {
		try {
			long count = produceStationService.count(new QueryWrapper<ProduceStation>().eq("station_code", produceStation.getStationCode()));
			if (count > 0) {
				return R.fail("生产工位编码已存在，请重新编辑！");
			}
			boolean flag = produceStationService.save(produceStation);

			return R.status(flag);
		} finally {
			OpcUaCache.clear();
		}
	}


	/**
	 * 新增或修改
	 * （前端修改用的这个接口）
	 */
	@PostMapping("/submit")
	@ApiOperation(value = "新增或修改", notes = "传入produceStation")
	public R submit(@Valid @RequestBody ProduceStation produceStation) {
		try {
			long count = produceStationService.count(new QueryWrapper<ProduceStation>().eq("station_code", produceStation.getStationCode())
				.ne("id", produceStation.getId()));
			if (count > 0) {
				return R.fail("生产工位编码已存在，请重新编辑！");
			}
			boolean flag = produceStationService.updateById(produceStation);
			return R.status(flag);
		} finally {
			OpcUaCache.clear();
		}
	}

	/**
	 * 新增或修改
	 * （前端修改用的这个接口）
	 */
	@PostMapping("/add")
	@ApiOperation(value = "新增", notes = "传入produceStation")
	public R add(@Valid @RequestBody ProduceStation produceStation) {
		try {
			long count = produceStationService.count(new QueryWrapper<ProduceStation>().eq("station_code", produceStation.getStationCode()));
			if (count > 0) {
				return R.fail("生产工位编码已存在，请重新编辑！");
			}
			produceStation.setId(IdUtil.getSnowflake(1, 2).nextId());
			boolean flag = produceStationService.save(produceStation);
			return R.status(flag);
		} finally {
			OpcUaCache.clear();
		}
	}

	/**
	 * 新增或修改
	 * （前端修改用的这个接口）
	 */
	@PostMapping("/edit")
	@ApiOperation(value = "新增或修改", notes = "传入produceStation")
	public R edit(@Valid @RequestBody ProduceStation produceStation) {
		try {
			long count = produceStationService.count(new QueryWrapper<ProduceStation>().eq("station_code", produceStation.getStationCode())
				.ne("id", produceStation.getId()));
			if (count > 0) {
				return R.fail("生产工位编码已存在，请重新编辑！");
			}
			boolean flag = produceStationService.updateById(produceStation);
			return R.status(flag);
		} finally {
			OpcUaCache.clear();
		}
	}

	/**
	 * 删除 生产工位
	 */
	@PostMapping("/remove")
	@ApiOperation(value = "逻辑删除", notes = "传入ids")
	public R remove(@ApiParam(value = "主键集合", required = true) @RequestParam String ids) {
		try {
			List<Long> idList = Func.toLongList(ids);
			ProduceStationVO produceStation = new ProduceStationVO();
			for (Long id : idList) {
				produceStation.setId(id);
				produceStationService.deleteById(produceStation);
			}
			return R.status(true);
		} finally {
			OpcUaCache.clear();
		}
	}

	@NotNull
	private ProduceStationVO buildCond(ProduceStationVO produceStationVO) {
		return QueryParaUtil.trim(produceStationVO);
	}

}
